﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

//function getXMLDatasource() {
//    var hDataSource = document.getElementById("datasourse");
//    return hDataSource.value;
//}

CKEDITOR.dialog.add('tablefield', function (editor) {
    var _caption = '';
    var _idName = '';
    var SelecctDatasource = DIF.DetailsData.getDetailsFieldData();

    var autoAttributes =
	{
	    value: 1,
	    size: 1,
	    maxLength: 1
	};

    var acceptedTypes =
	{
	    text: 1,
	    tablefield: 1
	};

    return {
        title: editor.lang.tablefield.title,
        minWidth: 350,
        minHeight: 200,
        onShow: function () {
            delete this.textField;

            var element = this.getParentEditor().getSelection().getSelectedElement();
            if (element && element.getName() == "input" &&
					(acceptedTypes[element.getAttribute('type')] || !element.getAttribute('type'))) {
                this.textField = element;
                this.setupContent(element);
            }
        },
        onOk: function () {
            var editor,
				element = this.textField,
				isInsertMode = !element;

            if (isInsertMode) {
                editor = this.getParentEditor();
                element = editor.document.createElement('input');
                element.setAttribute('type', 'tablefield');
                element.setAttribute('style', 'border-bottom-style: none; border-bottom-color: transparent; border-right-style: none; border-top-color: transparent; border-top-style: none; border-right-color: transparent; border-left-style: none; border-left-color: transparent');

            }

            if (isInsertMode)
                editor.insertElement(element);
            this.commitContent({ element: element });
        },
        onLoad: function () {
            var autoSetup = function (element) {
                var value = element.hasAttribute(this.id) && element.getAttribute(this.id);
                this.setValue(value || '');
            };

            var autoCommit = function (data) {
                var element = data.element;
                var value = this.getValue();

                if (value)
                    element.setAttribute(this.id, value);
                else
                    element.removeAttribute(this.id);
            };

            this.foreach(function (contentObj) {
                if (autoAttributes[contentObj.id]) {
                    contentObj.setup = autoSetup;
                    contentObj.commit = autoCommit;
                }
            });
        },
        contents: [
			{
			    id: 'info',
			    label: editor.lang.tablefield.title,
			    title: editor.lang.tablefield.title,
			    elements: [

                    {
                        id: 'datafields',
                        type: 'select',
                        style: 'width:100%;',
                        label: editor.lang.tablefield.datafields,
                        'default': 'text',
                        accessKey: 'D',
                        items: SelecctDatasource
                        ,
                        onChange: function (obj, element) {
                            var isExist = $.inArray(this.getValue(), ControlArray);
                            if (isExist < 0) {
                                var dialog = this.getDialog();
                                var selectValue = this.getValue();

                                var Item = jlinq.from(this.items).equals('1', selectValue).select();
                                if (typeof (Item) != "undefined") {
                                    if (typeof (Item[0]) != "undefined") { _caption = Item[0][0]; }
                                }

                                var textfieldName = dialog.getContentElement('info', '_cke_saved_name');
                                textfieldName.setValue(selectValue);
                                textfieldName.getElement().show();
                            } else {
                                if (obj.data.value != _idName) {
                                    alert(editor.lang.select.isexist_datafields);
                                }
                            }
                        },
                        setup: function (element) {
                            _idName = element.getAttribute('id');
                            this.setValue(element.getAttribute('datafields'));
                        },
                        commit: function (data) {
                            var element = data.element;

                            if (this.getValue()) {
                                element.setAttribute('datafields', this.getValue());
                                element.setAttribute('title', _caption);
                            }
                            else {
                                element.removeAttribute('datafields');
                                element.removeAttribute('title');
                            }
                        }
                    },
					{
					    type: 'hbox',
					    widths: ['50%', '50%'],
					    children:
						[
							{
							    id: '_cke_saved_name',
							    type: 'text',
							    label: editor.lang.tablefield.name,
							    'default': '',
							    className: 'cke_disabled',
							    accessKey: 'N',
							    onLoad: function () {
							        this.getInputElement().setAttribute('readOnly', true);
							    },
							    setup: function (element) {
							        this.setValue(
											element.data('cke-saved-name') ||
											element.getAttribute('name') ||
											'');
							    },
							    commit: function (data) {
							        var element = data.element;

							        if (this.getValue()) {
							            element.data('cke-saved-name', this.getValue());
							            element.setAttribute('id', this.getValue());
							            element.setAttribute('value', '@'+this.getValue());
							            element.setAttribute('class', 'textInput large');
							        }
							        else {
							            element.data('cke-saved-name', false);
							            element.removeAttribute('name');
							            element.removeAttribute('id');
							            element.removeAttribute('value');
							            element.removeAttribute('class');
							        }
							    }
							}
				
						]
					}
				
					
				]
			}
		]
    };
});
